﻿using DTcms.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DTcms.Web.api.payment.paypal
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string order_no = DTRequest.GetFormString("pay_order_no").ToUpper(); //订单号
            decimal order_amount = DTRequest.GetFormDecimal("pay_order_amount", 0); //订单金额
            string user_name = DTRequest.GetFormString("pay_user_name"); //支付用户名
            string subject = DTRequest.GetFormString("pay_subject"); //备注说明
            string trans_type = string.Empty; //交易类型1实物2虚拟

            if (order_no == "" || order_amount == 0)
            {
                Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您提交的参数有误！")));
                return;
            }

            if (order_no.StartsWith("R")) //R开头为在线充值订单
            {
                Model.user_recharge model = new BLL.user_recharge().GetModel(order_no);
                if (model == null)
                {
                    Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您充值的订单号不存在或已删除！")));
                    return;
                }
                if (model.amount != order_amount)
                {
                    Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您充值的订单金额与实际金额不一致！")));
                    return;
                }
                trans_type = "2";
            }
            else
            {
                Model.orders model = new BLL.orders().GetModel(order_no);
                if (model == null)
                {
                    Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您支付的订单号不存在或已删除！")));
                    return;
                }
                if (model.order_amount != order_amount)
                {
                    Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您支付的订单金额与实际金额不一致！")));
                    return;
                }
                trans_type = "1";
            }

            //检查会员还是匿名
            if (!string.IsNullOrEmpty(user_name))
            {
                user_name = "支付会员：" + user_name;
            }
            else
            {
                user_name = "匿名用户";
            }

            var paySubmit = new API.Payment.paypal.PaypalSumbit();
            string number = order_no;
            string goodsname = (trans_type.Equals("1") ? "商品购买" : "在线充值") + "，订单号：" + order_no;
            string price = order_amount.ToString("0.00");
            string currency = "USD";
            string quantity = "1";

            var resultUrl = "";
            try
            {
                string token = paySubmit.GetPayToken(number, goodsname, price, currency, quantity);
                Model.orders model = new BLL.orders().GetModel(order_no);
                model.trade_no = token;
                new BLL.orders().UpdateField(model.id, $"trade_no='{token}'");
                resultUrl = paySubmit.GetPayUrlByToken(token);
            }
            catch (Exception ex)
            {
                resultUrl = new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode(ex.Message));
            }

            try
            {
                Response.Redirect(resultUrl);
            }
            catch (Exception)
            {

            }
        }
    }
}